<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息</title>
    <!-- Bootstrap CSS 链接 -->
    <link rel="stylesheet" href="./common/css/bootstrap.min.css">
    <style>
        .img-fluid {
            width: 200px;
            height: 200px;
            margin-bottom: 20px;
        }
    </style>
</head>

<body>

    <?php 
    // 引入数据库连接代码
    include 'conn.php';
    include 'header.php';
    include 'function.php';

    // 获取当前用户的信息（假设用户已登录，可以从 session 中获取用户ID）
    $user_id = $_SESSION['user_id']; // 假设用户ID存储在 session 中

    // 获取用户信息的函数
    function getUserInfo($conn, $user_id)
    {
        $sql = "SELECT * FROM users WHERE user_id = $user_id";
        $result = executeQuery($conn, $sql);

        if ($result && mysqli_num_rows($result) > 0) {
            $user = mysqli_fetch_assoc($result);
            mysqli_free_result($result);

            return [
                'username' => $user['username'],
                'email' => $user['email'],
                'phone' => $user['phone'],
                'image_path' => $user['image_path'],
            ];
        } else {
            // 处理未找到用户的情况
            return null;
        }
    }

    // 获取用户信息
    $userInfo = getUserInfo($conn, $user_id);

    // 如果未找到用户信息，进行相应处理
    if ($userInfo === null) {
        echo '未找到用户信息';
        exit();
    }

    // 使用 $userInfo 数组中的数据
    $username = $userInfo['username'];
    $email = $userInfo['email'];
    $phone = $userInfo['phone'];
    $image_path = $userInfo['image_path'];
    ?>


    <div class="container mt-4">
        <h2>个人信息</h2>

        <!-- 省略其他代码... -->

        <div class="row">
            <div class="col-md-6">
                <!-- 头像显示 -->
                <img src="<?php echo empty($image_path) ? './images/touxiang/default.jpg' : $image_path; ?>" alt="头像" class="img-fluid rounded-circle">
                <!-- 修改头像按钮，触发头像上传功能 -->
                <form action="update_touxiang.php" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="avatar">上传新头像</label>
                        <input type="file" class="form-control-file" id="avatar" name="avatar">
                    </div>
                    <br>
                    <button type="submit" class="btn btn-primary">修改头像</button>
                </form>
                <div class="col-md-8  mt-3">
                    <div class="card shadow">
                        <div class="card-body">
                            <h4 class="card-title">用户信息</h4>
                            <hr>

                            <div class="row">
                                <div class="col-md-4">
                                    <label class="font-weight-bold">用户名:</label>
                                </div>
                                <div class="col-md-8">
                                    <p><?php echo $username; ?></p>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-4">
                                    <label class="font-weight-bold">Email:</label>
                                </div>
                                <div class="col-md-8">
                                    <p><?php echo $email; ?></p>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-4">
                                    <label class="font-weight-bold">电话:</label>
                                </div>
                                <div class="col-md-8">
                                    <p><?php echo $phone; ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <!-- 修改信息表单 -->
                <form action="update_info.php" method="post">
                    <div class="card shadow mb-4">
                        <div class="card-body">
                            <h5 class="card-title">修改信息</h5>
                            <div class="form-group">
                                <label for="new_email">新邮箱地址</label>
                                <input type="email" class="form-control" id="new_email" name="new_email" required>
                            </div>
                            <div class="form-group">
                                <label for="new_phone">新电话号码</label>
                                <input type="tel" class="form-control" id="new_phone" name="new_phone" required>
                            </div>
                            <br>
                            <button type="submit" class="btn btn-primary">保存修改</button>
                        </div>
                    </div>
                </form>

                <!-- 修改密码表单 -->
                <form action="update_pwd.php" method="post">
                    <div class="card shadow">
                        <div class="card-body">
                            <h5 class="card-title">修改密码</h5>
                            <div class="form-group">
                                <label for="new_password">新密码</label>
                                <input type="password" class="form-control" id="new_password" name="new_password" required>
                            </div>
                            <br>
                            <button type="submit" class="btn btn-primary">修改密码</button>
                        </div>
                    </div>
                </form>
            </div>

        </div>
        <?php
        // 关闭数据库连接
        mysqli_close($conn);
        ?>
</body>

</html>